import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hxzy.util.MybatisUtil;
import com.hxzy.dto.UserInfoDTO;
import com.hxzy.entity.UserInfo;
import com.hxzy.mapper.UserInfoMapper;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.mindrot.jbcrypt.BCrypt;

import java.util.List;

/**
 * @Description:
 * @Author: ykj
 * @Date: 2022/2/24 13:55
 */

public class UserInfotest {

    @Test
    public void insert(){
        //创建一个对象
        UserInfo userInfo=new UserInfo();
        userInfo.setId(2);
        userInfo.setLoginName("李四");
        //密码加密
        userInfo.setLoginPwd(BCrypt.hashpw("test8888", BCrypt.gensalt()));
        userInfo.setStatus(1);
        //保存的数据库中
        try(
            SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()
        ){
            UserInfoMapper userInfoMapperImpl=sqlSession.getMapper(UserInfoMapper.class);
            //调用新增方法
            int count=userInfoMapperImpl.insert(userInfo);
            if (count>0){
                //提交事务
                sqlSession.commit();
                System.out.println("新增成功");
            }else {
                //回滚事物
                sqlSession.rollback();
                System.out.println("新增失败");
            }
        }
    }

    @Test
    public void update(){
        UserInfo userInfo=new UserInfo();
        userInfo.setStatus(0);
        userInfo.setLoginName("牛魔王");
        userInfo.setLoginPwd("132465");
        userInfo.setId(2);
        try (
            SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()
        ){
            UserInfoMapper userInfoMapperimpl=sqlSession.getMapper(UserInfoMapper.class);
            int update = userInfoMapperimpl.update(userInfo);
            if (update>0){
                sqlSession.commit();
                System.out.println("更改成功");
            }else {
                sqlSession.rollback();
                System.out.println("更改失败");
            }
        }
    }

    @Test
    public void test(){
        try(
                SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()
        ){
            UserInfoMapper userInfoMapperimpl=sqlSession.getMapper(UserInfoMapper.class);
            int delete = userInfoMapperimpl.delete(2);
            if (delete>0){
                sqlSession.commit();
                System.out.println("提交成功");
            }else {
                sqlSession.rollback();
                System.out.println("提交失败");
            }
        }
    }

    @Test
    public void findOne(){
        try (
                SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession();
                ){
                UserInfoMapper userInfoMapperimpl=sqlSession.getMapper(UserInfoMapper.class);
            UserInfo one = userInfoMapperimpl.findOne(1);
            System.out.println(one);
        }
    }

    @Test
    public void findAll(){
        UserInfoDTO userInfo=new UserInfoDTO();
        userInfo.setPage(1);
        userInfo.setSize(10);
//        userInfo.setLoginName("孙");
        userInfo.setStatus(0);
        //做分页插件
        PageHelper.startPage(userInfo.getPage(),userInfo.getSize());

        try(
                SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()
                ){
            UserInfoMapper mapper = sqlSession.getMapper(UserInfoMapper.class);

            //再执行你写的sql语句 ，List不再是ArrayList，而是Page对象
            List<UserInfo> all = mapper.findAll(userInfo);
            Page<UserInfo> pg= (Page<UserInfo>) all;
            System.out.println(pg);

        }
    }
}
